var result_code_list = [];//记录每次查询返回的ts_code_list
var dic_condtions_list = [];//记录条件选股的条件

/*
根据连续天数、资金流入流出查询。
 */
function select_money_inflow(days, in_out) {
    var query_code_list = [];
    if (($("#checkbox_inflow")[0].checked) || ($("#checkbox_outflow")[0].checked)) {
        query_code_list = result_code_list;
    }
    $.ajax({
        url: "./",
        type: "GET",
        data: {
            cmd_class: "query",//cmd_class表示返回命令类别
            command: "money_inflow",//command表示具体返回的命令，
            continuedays: days,
            in_or_out: in_out,
            query_code_list: JSON.stringify(query_code_list)//已有查询结果列表，如果为空，表示不在现查询结果中查询
        },
        success: function (callback) {
            var data = eval(callback);

            $("#div_showdata").empty();
            if (in_out == 'in') {
                $("#div_showdata").append("<div class='card-header'>共" + data[0].length + "支股票连续" + days + "天资金流入股票 </div>");

            } else {
                $("#div_showdata").append("<div class='card-header'>共" + data[0].length + "支股票连续" + days + "天资金流出股票 </div>");
            }

            $("#div_showdata").append("<div id='ul_list'></div>");
            additem = $("#ul_list");

            for (i = 0; i < data[0].length; ++i) {
                additem.append("<li><input type='checkbox'/>&nbsp;<a  href='#' onclick='openIndexWithStockName(this)'>" + data[0][i].trim() + ":" + data[1][i].trim() + "</a></li>");
            }

            $("footer").before("<div class=\"row\" style=\"width: 100%\"></div>\n" +
                "<div class=\"text-center\" style=\"width: 100%;\"></div>");//在footer之前插入了两个div标签，便于居中
            $("#div_showdata").next().next().empty();//过两个兄弟标签，才能居中
            if (data[0].length > 0) {

                $("#div_showdata").next().next().append("<div align='center'><button class='btn btn-secondary' onclick='select_all_checked_eligible_stock()'>关注选中股票</button></div>");
            }

            //记录查询返回结果code_list
            result_code_list = data[0];

        },
        error: function () {
            alert('提交失败');
        }
    })
}


/*
根据连续天数、股票涨跌查询。
 */
function select_by_updown(days, up_down) {
    var query_code_list = [];
    if (($("#checkbox_updown_up")[0].checked) || ($("#checkbox_updown_down")[0].checked)) {
        query_code_list = result_code_list;
    }
    $.ajax({
        url: "./",
        type: "GET",
        data: {
            cmd_class: "query",
            command: "by_updown",
            continuedays: days,
            updown: up_down,
            query_code_list: JSON.stringify(query_code_list)
        },
        success: function (callback) {
            var data = eval(callback);
            $("#div_showdata").empty();
            if (up_down == 'up') {
                $("#div_showdata").append("<div class='card-header col-xl-2'>共" + data[0].length + "支股票连续" + days + "天上涨 </div>");
            } else {
                $("#div_showdata").append("<div class='card-header col-xl-2'>共" + data[0].length + "支股票连续" + days + "天下跌 </div>");
            }

            $("#div_showdata").append("<div id='ul_list'></div>");
            additem = $("#ul_list");
            for (i = 0; i < data[0].length; ++i) {
                additem.append("<li><input type='checkbox'/>&nbsp;<a  href='#' onclick='openIndexWithStockName(this)'>" + data[0][i].trim() + ":" + data[1][i].trim() + "</a></li>");
            }

            $("footer").before("<div class=\"row\" style=\"width: 100%\"></div>\n" +
                "<div class=\"text-center\" style=\"width: 100%;\"></div>");//在footer之前插入了两个div标签，便于居中
            $("#div_showdata").next().next().empty();//过两个兄弟标签，才能居中
            if (data[0].length > 0) {

                $("#div_showdata").next().next().append("<div align='center'><button class='btn btn-secondary' onclick='select_all_checked_eligible_stock()'>关注选中股票</button></div>");
            }
            //记录查询返回结果code_list
            result_code_list = data[0];
        },
        error: function () {
            alert('提交失败');
        }
    })
}


/*
根据连续天数，股票连续涨跌幅度查询。
 */
function select_by_pct_chg(days, up_down, pct_chg) {
    var query_code_list = [];
    if (($("#checkbox_up_pct_chg")[0].checked) || ($("#checkbox_down_pct_chg")[0].checked)) {
        query_code_list = result_code_list;
    }
    $.ajax({
        url: "./",
        type: "GET",
        data: {
            cmd_class: "query",
            command: "by_pct_chg",
            continuedays: days,
            updown: up_down,
            pct_chg: pct_chg,
            query_code_list: JSON.stringify(query_code_list)
        },
        success: function (callback) {
            var data = eval(callback);
            $("#div_showdata").empty();
            if (up_down == 'up') {
                $("#div_showdata").append("<div class='card-header col-xl-2'>共" + data[0].length + "支股票连续" + days + "天连续上涨" + pct_chg + "% </div>");
            } else {
                $("#div_showdata").append("<div class='card-header col-xl-2'>共" + data[0].length + "支股票连续" + days + "天连续下跌" + pct_chg + "% </div>");
            }
            $("#div_showdata").append("<div id='ul_list'></div>");
            // $("#div_showdata").append("<div id='ul_list'></div>");
            additem = $("#ul_list");
            for (i = 0; i < data[0].length; ++i) {
                additem.append("<li><input type='checkbox'/>&nbsp;<a  href='#' onclick='openIndexWithStockName(this)'>" + data[0][i].trim() + ":" + data[1][i].trim() + "</a></li>");
            }

            $("footer").before("<div class=\"row\" style=\"width: 100%\"></div>\n" +
                "<div class=\"text-center\" style=\"width: 100%;\"></div>");//在footer之前插入了两个div标签，便于居中
            $("#div_showdata").next().next().empty();//过两个兄弟标签，才能居中
            if (data[0].length > 0) {

                $("#div_showdata").next().next().append("<div align='center'><button class='btn btn-secondary' onclick='select_all_checked_eligible_stock()'>关注选中股票</button></div>");
            }
            //记录查询返回结果code_list
            result_code_list = data[0];
        },
        error: function () {
            alert('提交失败');
        }
    })
}


/***********************
 *根据条件选股
 ***********************/
function query_with_conditions() {
    //判断是否有checkbox被选中
    is_no_checked = true;
    $("#div_conditions input[type=checkbox]").each(function () {
        if ($(this).is(':checked')) {
            is_no_checked = false;
        }
    });
    //如果没有一个被选中，则退出
    if (is_no_checked) {
        alert('请选择至少一个查询条件');
        return
    }

    //获取交易日期，如果未选择，默认系统当天日期
    trade_date = '';
    if ($("#checkbox_trade_date")[0].checked) {
        if ($("#date_trade_date")[0].value.trim() == "") {
            alert('请输入交易日期');
            return false;
        }
        trade_date = $("#date_trade_date")[0].value.trim();
        trade_date = trade_date.replace(RegExp("-", "g"), '')//去除字符串中的”-“
    } else {
        date = new Date();
        year1 = date.getFullYear();
        month1 = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
        day1 = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
        trade_date = year1.toString() + month1.toString() + day1.toString();//格式化成YYYYMMDD格式
    }

    //清空数据
    dic_condtions_list = [];
    if (!collect_conditions()) {
        return;
    }
    // alert(dic_conditions.operation_pct_chg)
    $.ajax({
        url: "./",
        type: "GET",
        data: {
            cmd_class: "query",
            command: "query_with_conditions",
            trade_date: trade_date,
            data: JSON.stringify(dic_condtions_list)
        },//cmd_class表示返回命令类别，command表示具体返回的命令，提高判断语句效率
        // data: {cmd_class: "query", command: "query_with_conditions", "trade_date":trade_date,'data': dic_condtions_list},
        success: function (callback) {
            var data = eval(callback);

            $("#div_showdata").empty();

            $("#div_showdata").append("<div class='align-content-center'>共" + data[0].length + "支股票符合条件</div>");

            $("#div_showdata").append("<div id='ul_list'></div>");
            additem = $("#ul_list");

            for (i = 0; i < data[0].length; ++i) {
                additem.append("<li><input type='checkbox'/>&nbsp;<a  href='#' onclick='openIndexWithStockName(this)'>" + data[0][i].trim() + ":" + data[1][i].trim() + "</a></li>");
            }

            $("footer").before("<div class=\"row\" style=\"width: 100%\"></div>\n" +
                "<div class=\"text-center\" style=\"width: 100%;\"></div>");//在footer之前插入了两个div标签，便于居中
            $("#div_showdata").next().next().empty();//过两个兄弟标签，才能居中
            if (data[0].length > 0) {

                $("#div_showdata").next().next().append("<div align='center'><button class='btn btn-secondary' onclick='select_all_checked_eligible_stock()'>关注选中股票</button></div>");
            }
            //记录查询返回结果code_list
            result_code_list = data[0];

        },
        error: function (e) {
            alert('按条件查询股票失败');

        }

    })


}


/*
调用index页面，并传递股票名称和代码过去，便于index页面执行查询操作
 */
function openIndexWithStockName(a) {
    stockinfo = a.innerHTML;
    var strs = stockinfo.split(":");
    var ts_code = strs[0].trim();
    var name = strs[1].trim();
    window.open("/stock/index/?analysis=true&ts_code=" + ts_code + "&name=" + name, 'newwindow');
}


/*******************
 * checkbox操作
 ********************/
function check_click(obj) {
    if (obj.id == 'checkbox_circ_mv') {
        if ($("#checkbox_circ_mv")[0].checked) {
            $("#select_circ_mv")[0].disabled = '';
            $("#num_circ_mv")[0].disabled = '';
        } else {
            $("#select_circ_mv")[0].disabled = 'disabled';
            $("#num_circ_mv")[0].disabled = 'disabled';
            // $("#num_circ_mv")[0].value = '';
        }
    }
    if (obj.id == 'checkbox_pe_ttm') {
        if ($("#checkbox_pe_ttm")[0].checked) {
            $("#select_pe_ttm")[0].disabled = '';
            $("#num_pe_ttm")[0].disabled = '';
        } else {
            $("#select_pe_ttm")[0].disabled = 'disabled';
            $("#num_pe_ttm")[0].disabled = 'disabled';
            // $("#num_pe_ttm")[0].value = '';
        }
    }
    if (obj.id == 'checkbox_main_in') {
        if ($("#checkbox_main_in")[0].checked) {
            $("#select_main_in")[0].disabled = '';
            $("#num_main_in")[0].disabled = '';
        } else {
            $("#select_main_in")[0].disabled = 'disabled';
            $("#num_main_in")[0].disabled = 'disabled';
            // $("#num_main_in")[0].value = '';
        }
    }
    if (obj.id == 'checkbox_main_turnover_rate') {
        if ($("#checkbox_main_turnover_rate")[0].checked) {
            $("#select_main_turnover_rate")[0].disabled = '';
            $("#num_main_turnover_rate")[0].disabled = '';
        } else {
            $("#select_main_turnover_rate")[0].disabled = 'disabled';
            $("#num_main_turnover_rate")[0].disabled = 'disabled';
            // $("#num_main_turnover_rate")[0].value = '';

        }
    }
    if (obj.id == 'checkbox_turnover_rate_f') {
        if ($("#checkbox_turnover_rate_f")[0].checked) {
            $("#select_turnover_rate_f")[0].disabled = '';
            $("#num_turnover_rate_f")[0].disabled = '';
        } else {
            $("#select_turnover_rate_f")[0].disabled = 'disabled';
            $("#num_turnover_rate_f")[0].disabled = 'disabled';
            // $("#num_turnover_rate_f")[0].value = '';
        }
    }
    if (obj.id == 'checkbox_pct_chg') {
        if ($("#checkbox_pct_chg")[0].checked) {
            $("#select_pct_chg")[0].disabled = '';
            $("#num_pct_chg")[0].disabled = '';
        } else {
            $("#select_pct_chg")[0].disabled = 'disabled';
            $("#num_pct_chg")[0].disabled = 'disabled';
            // $("#num_pct_chg")[0].value = '';
        }
    }
    if (obj.id == 'checkbox_trade_date') {
        if ($("#checkbox_trade_date")[0].checked) {

            $("#date_trade_date")[0].disabled = '';
        } else {
            $("#date_trade_date")[0].disabled = 'disabled';
        }
    }
    if (obj.id == 'checkbox_net_mf_amount') {
        if ($("#checkbox_net_mf_amount")[0].checked) {
            $("#select_net_mf_amount")[0].disabled = '';
            $("#num_net_mf_amount")[0].disabled = '';
        } else {
            $("#select_net_mf_amount")[0].disabled = 'disabled';
            $("#num_net_mf_amount")[0].disabled = 'disabled';
        }
    }

}


/********************************************
 * 收集选股条件，存入条件字典dic_conditions中
 ********************************************/
function collect_conditions() {
    //用个字典做数据传导

    if ($("#checkbox_pct_chg")[0].checked) {
        //todo:需要对输入值做判断
        if ($("#num_pct_chg")[0].value.trim() == "") {
            alert('请输入涨幅数据');
            return false;
        }
        dic_conditions = {};
        dic_conditions['info'] = 'pct_chg';
        dic_conditions['operation'] = $("#select_pct_chg")[0].value.trim();
        dic_conditions['value'] = $("#num_pct_chg")[0].value.trim();
        dic_condtions_list.push(dic_conditions);
    }
    if ($("#checkbox_turnover_rate_f")[0].checked) {
        if ($("#num_turnover_rate_f")[0].value.trim() == "") {
            alert('请输入换手率');
            return false;
        }
        dic_conditions = {};
        dic_conditions['info'] = 'turnover_rate_f';
        dic_conditions['operation'] = $("#select_turnover_rate_f")[0].value.trim();
        dic_conditions['value'] = $("#num_turnover_rate_f")[0].value.trim();
        dic_condtions_list.push(dic_conditions);
    }
    if ($("#checkbox_circ_mv")[0].checked) {
        if ($("#num_circ_mv")[0].value.trim() == "") {
            alert('请输入流动市值（单位:万元）');
            return false;
        }
        dic_conditions = {};
        dic_conditions['info'] = 'circ_mv';
        dic_conditions['operation'] = $("#select_circ_mv")[0].value.trim();
        dic_conditions['value'] = $("#num_circ_mv")[0].value.trim();
        dic_condtions_list.push(dic_conditions);
    }
    if ($("#checkbox_main_in")[0].checked) {
        if ($("#num_main_in")[0].value.trim() == "") {
            alert('请输入主力净流入值');
            return false;
        }
        dic_conditions = {};
        dic_conditions['info'] = 'main_in';
        dic_conditions['operation'] = $("#select_main_in")[0].value.trim();
        dic_conditions['value'] = $("#num_main_in")[0].value.trim();
        dic_condtions_list.push(dic_conditions);
    }
    if ($("#checkbox_main_turnover_rate")[0].checked) {
        if ($("#num_main_turnover_rate")[0].value.trim() == "") {
            alert('请输入主力占换手比例');
            return false;
        }
        dic_conditions = {};
        dic_conditions['info'] = 'main_turnover_rate';
        dic_conditions['operation'] = $("#select_main_turnover_rate")[0].value.trim();
        dic_conditions['value'] = $("#num_main_turnover_rate")[0].value.trim();
        dic_condtions_list.push(dic_conditions);
    }
    if ($("#checkbox_net_mf_amount")[0].checked) {
        if ($("#num_net_mf_amount")[0].value.trim() == "") {
            alert('请输入总流入资金金额');
            return false;
        }
        dic_conditions = {};
        dic_conditions['info'] = 'net_mf_amount';
        dic_conditions['operation'] = $("#select_net_mf_amount")[0].value.trim();
        dic_conditions['value'] = $("#num_net_mf_amount")[0].value.trim();
        dic_condtions_list.push(dic_conditions);
    }
    if ($("#checkbox_pe_ttm")[0].checked) {
        if ($("#num_pe_ttm")[0].value.trim() == "") {
            alert('请输入市盈率');
            return false;
        }
        dic_conditions = {};
        dic_conditions['info'] = 'pe_ttm';
        dic_conditions['operation'] = $("#select_pe_ttm")[0].value.trim();
        dic_conditions['value'] = $("#num_pe_ttm")[0].value.trim();
        dic_condtions_list.push(dic_conditions);
    }


    return true;
}


/*
* 快捷查询相互冲突检测
* */
function merge_query_check(obj) {
    if (obj.id == 'checkbox_inflow') {
        $("#checkbox_outflow")[0].checked = false;
    }

    if (obj.id == 'checkbox_outflow') {
        $("#checkbox_inflow")[0].checked = false;
    }

    if (obj.id == 'checkbox_updown_up') {
        $("#checkbox_updown_down")[0].checked = false;
    }

    if (obj.id == 'checkbox_updown_down') {
        $("#checkbox_updown_up")[0].checked = false;
    }

    if (obj.id == 'checkbox_down_pct_chg') {
        $("#checkbox_up_pct_chg")[0].checked = false;
    }

    if (obj.id == 'checkbox_up_pct_chg') {
        $("#checkbox_down_pct_chg")[0].checked = false;
    }
}


/*
将选中需要关注的股票存入数据库
 */
function select_all_checked_eligible_stock() {
    //查询div_showdata中所有checkbox
    stock_selected = $("#div_showdata input[type='checkbox']");
    attention_code_list = [];
    for (i = 0; i < stock_selected.length; i++) {
        if (stock_selected[i].checked) {
            var one = stock_selected[i].nextElementSibling || stock_selected[i].nextSibling;//查找checkbox的兄弟节点
            str = one.innerHTML;
            code = str.split(':')[0];
            stockname = str.split(':')[1];
            attention_code_list.push(code);//将所有选中股票代码存入列表中，发给数据库存储
        }
    }
    if (attention_code_list.length == 0) {
        alert('请选择需要关注的股票！！！');
        return;
    }

    //获取交易日期，如果未选择，默认系统当天日期
    trade_date = '';
    if ($("#checkbox_trade_date")[0].checked) {
        if ($("#date_trade_date")[0].value.trim() == "") {
            alert('请输入交易日期');
            return false;
        }
        trade_date = $("#date_trade_date")[0].value.trim();
        trade_date = trade_date.replace(RegExp("-", "g"), '')//去除字符串中的”-“
    } else {
        date = new Date();
        year1 = date.getFullYear();
        month1 = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
        day1 = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
        trade_date = year1.toString() + month1.toString() + day1.toString();//格式化成YYYYMMDD格式
    }


    $.ajax({
        url: "./",
        type: "POST",
        data: {
            cmd_class: "write_db",
            command: "attention_code",
            trade_date: trade_date,
            data: JSON.stringify(attention_code_list),
        },
        success: function (callback) {
            if (callback) {
                alert('提交关注股票成功');
            } else {
                alert('提交关注股票失败');
            }
        },
        error: function (e) {
            alert('提交关注股票失败');

        }
    })


}

